package com.sansecy.customview.view.paystatus;

import android.animation.Animator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sansecy.customview.DensityUtils;
import com.sansecy.customview.R;

/**
 * 项目名称：supermarket
 * 类 名 称：PayStatusView
 * 类 描 述：副屏支付状态视图
 * 默认子视图是空的，发生支付时会添加子视图
 * 创建时间：2019-11-07 14:56
 * 创 建 人：sansecy
 */
public class PayStatusView extends LinearLayout {

    private boolean mIsDrawBackground = false;

    private Drawable mBackgroundDrawable;
    private Animator.AnimatorListener mAnimatorListener;

    public PayStatusView(Context context) {
        super(context);
        init();
    }

    public PayStatusView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void setAnimatorListener(Animator.AnimatorListener animatorListener) {
        mAnimatorListener = animatorListener;
    }

    private void init() {
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);
        mBackgroundDrawable = getResources().getDrawable(R.drawable.bg_second_display_gradient_billing);
    }

    public void setDrawBackground(boolean drawBackground) {
        mIsDrawBackground = drawBackground;
    }

    private int dp100 = DensityUtils.dp2px(getContext(), 100);
    private int dp108 = DensityUtils.dp2px(getContext(), 108);
    private int dp28 = DensityUtils.dp2px(getContext(), 28);
    private int dp7 = DensityUtils.dp2px(getContext(), 7);

    private int successColor = Color.parseColor("#3f6ac9");
    private int failedColor = Color.parseColor("#f2523e");
    private int payingColor = Color.parseColor("#333333");

    public void showPaySuccess(String price) {
        removeAllViews();
        setDrawBackground(true);
        //动画图
        PaySuccessView view = new PaySuccessView(getContext());
        view.addAnimatorListener(mAnimatorListener);
        view.setColor(successColor);
        view.setStrokeWidth(dp7);
        int dp100 = DensityUtils.dp2px(getContext(), 100);
        LayoutParams params = new LayoutParams(dp100, dp100);
        params.gravity = Gravity.CENTER;
        view.setLayoutParams(params);
        addView(view);
        //提示文字
        addView(createTitleView("收款成功", successColor));
        //金额
        addView(createHitView(String.format("收款金额：¥ %s", price), successColor));
    }

    public void showPayFailed() {
        removeAllViews();
        setDrawBackground(true);
        PayFailedView view = new PayFailedView(getContext());
        view.addAnimatorListener(mAnimatorListener);
        view.setColor(Color.parseColor("#FF5D48"));
        view.setStrokeWidth(dp7);
        LayoutParams params = new LayoutParams(dp100, dp100);
        params.gravity = Gravity.CENTER;
        view.setLayoutParams(params);
        view.setColor(failedColor);
        addView(view);
        addView(createTitleView("收款失败", failedColor));
        addView(createHitView("请等待老板重新发起收款", failedColor));
    }

    public void showPaying() {
        removeAllViews();
        setDrawBackground(true);
        PayingView view = new PayingView(getContext());
        view.setRadius(10);
        view.addAnimatorListener(mAnimatorListener);
        LayoutParams params = new LayoutParams(dp108, dp108);
        params.gravity = Gravity.CENTER;
        view.setLayoutParams(params);
        addView(view);
        addView(createTitleView("等待支付结果..", payingColor));
        addView(createHitView("支付尚未完成，请勿离开", payingColor));
    }

    private TextView createTitleView(String text, int color) {
        TextView tvTitle = new TextView(getContext());
        LayoutParams params = new LayoutParams(-2, -2);
        params.topMargin = dp28;
        tvTitle.setLayoutParams(params);
        tvTitle.setText(text);
        tvTitle.setTextSize(40);
        tvTitle.setTextColor(color);
        return tvTitle;
    }

    private TextView createHitView(String text, int color) {
        TextView tvHit = new TextView(getContext());
        tvHit.setLayoutParams(new LayoutParams(-2, -2));
        tvHit.setText(text);
        tvHit.setTextSize(24);
        tvHit.setTextColor(color);
        return tvHit;
    }

    public void hideAll() {
        removeAllViews();
        setDrawBackground(false);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        if (mIsDrawBackground) {
            mBackgroundDrawable.setBounds(0, 0, getWidth(), getHeight());
            mBackgroundDrawable.draw(canvas);
        }
        super.dispatchDraw(canvas);
    }

}
